﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcShop.Models;
using MvcShop.Extensions;
using MvcShop.Extensions.Html;
using System.Linq.Expressions;
using Microsoft.Web.Mvc;
using MvcShop.MVC.Controllers;
using MvcShop.MVC.Areas.Admin.Controllers;
using MvcShop.Models.ViewDataModels;

namespace MvcShop.MVC.HtmlHelpers
{
    public static class UrlHelperExtensions
    {
        #region ==== Ads ====
        public static string ToAds(this UrlHelper urlHelper, string mark, int? nums)
        {
            if (nums != null)
                return urlHelper.RouteUrl("ads", new { controller = "Ads", action = "ListAd", id = mark, num = nums });
            else
                return urlHelper.RouteUrl("Default", new { controller = "Ads", action = "SingleAd", id = mark });
        }
        #endregion

        #region ==== SearchForm ====
        public static string BeginSearchForm(this UrlHelper helper, string actionName, string formId)
        {
            return "<form action='{0}' id='{1}' method='get'>".With(helper.RouteUrl("Default", new { controller = "Search", action = actionName }), formId);
        }

        public static string EndSearchForm(this UrlHelper helper)
        {
            return "</form>";
        }
        #endregion

        #region ==== Category ====
        public static string WriteCategories(this UrlHelper urlHelper, IEnumerable<Category> rootCategory, CategoryDisplay display)
        {
            var categoryWriter = new CategoryWriter(rootCategory, urlHelper, display);
            return categoryWriter.Write();
        }

        public static string ToCategoriesUpArrowLink(this UrlHelper urlHelper, int id)
        {
            return StringExtensions.With("<a href=\"{0}\" class=\"arrowlink\">{1}</a>", urlHelper.Of<CategoriseManageController>().Action(c => c.MoveUp, id),
                urlHelper.Image("Move Up", "~/Content/Images/Up.png"));
        }

        public static string ToCategoriesDownArrowLink(this UrlHelper urlHelper, int id)
        {
            return "<a href=\"{0}\" class=\"arrowlink\">{1}</a>".With(
                urlHelper.Of<CategoriseManageController>().Action(c=>c.MoveDown,id),
                urlHelper.Image("Move Down", "~/Content/Images/Down.png"));
        }
        #endregion

        #region ==== UsersHome ====
        public static string ToUsersHome(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Users_default", new { controller = "UsersHome", action = "Index" });
        }
        #endregion

        #region ==== GeneralUrl ====
        //public static string GeneralUrl<T>(this UrlHelper urlHelper, Func<TController, Action<T>> action) where T : Controller
        //{
        //    return urlHelper.RouteUrl("Account");
        //}
        #endregion

        #region ==== LogOn ====
        public static string ToLogOn(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Account", new { controller = "LoinAccount", action = "LogOn" });
        }
        public static string ToLogOff(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Account", new { controller = "LoinAccount", action = "LogOff" });
        }
        #endregion

        #region ==== Home ====
        public static string ToHome(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Home", action = "Index" });
        }

        public static string ToHome(this UrlHelper urlHelper,string actionName)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Home", action = actionName });
        }
        #endregion

        #region ==== Products ====
        public static string ToListProduct(this UrlHelper urlHelper, int GategoryId)
        {
            return urlHelper.RouteUrl("ProductList", new { gategoryId = GategoryId });
        }

        public static string ToProduct(this UrlHelper urlHelper, int GategoryId, int ProductId)
        {
            return urlHelper.RouteUrl("Products", new { gategoryId = GategoryId, productId = ProductId });
        }
        #endregion

        #region ==== UserRemark ====
        public static string ToUserRemark(this UrlHelper urlHelper,int productId)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Product", action = "UserRemark", id = productId });
        }

        public static string ToAddRemark(this UrlHelper urlHelper, int productId)
        {
            return urlHelper.RouteUrl("Users_default", new { controller = "UsersRemark", action = "AddRemark", id = productId });
        }

        public static string ToAddConsultative(this UrlHelper urlHelper, int productId)
        {
            return urlHelper.RouteUrl("Users_default", new { controller = "UsersRemark", action = "AddConsultative", id = productId });
        }
        #endregion

        #region ==== ShoppingCart =====
        public static string ToShoppingCart(this UrlHelper urlHelper, int productId, int? qua)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Product", action = "UpdateSC", id = productId, quantity = qua });
        }

        public static string ToShoppingCart(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Product", action = "ShoppingCart" });
        }

        public static string ToDelShoppingCart(this UrlHelper urlHelper, int productId)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Product", action = "DelProductToCart", id = productId });
        }

        public static string ToDelAllShoppingCart(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("Default", new { controller = "Product", action = "DelAllCart" });
        }
        #endregion

        #region ==== News ====
        public static string ToNews(this UrlHelper urlHelper, int NewsId, DateTime Date)
        {
            //return urlHelper.RouteUrl("News", new { newsId = NewsId, date = Date });
            return urlHelper.RouteUrl("News", new { newsId = NewsId, year = Date.Year, month = Date.Month, day = Date.Day });
        }
        #endregion

        #region ==== Favorites =====
        public static string ToFavorites(this UrlHelper urlHelper, int productId)
        {
            return urlHelper.RouteUrl("Users_default", new { controller = "UsersHome", action = "AddFavorites", id = productId });
        }
        #endregion
    }
}
